package activiti;

import java.util.List;

import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;

public class GroupTask {
	//配置流程引擎对象
	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	/**
	 * 部署流程定义
	 */
	@Test
	public void deployment(){
		DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment();
		//加载流程文件
		deploymentBuilder.addClasspathResource("bxlc.bpmn");
		deploymentBuilder.addClasspathResource("bxlc.png");
		//执行部署
		Deployment deploy = deploymentBuilder.deploy();
	}
	@Test
	/**
	 * 启动流程实例
	 */
	public void startInstance(){
		//获取流程定义的key,以启动流程实例
		ProcessDefinitionQuery definitionQuery = processEngine.getRepositoryService().createProcessDefinitionQuery();
		List<ProcessDefinition> list = definitionQuery.list();
		String defKey=list.get(0).getKey();
		//根据流程定义id启动实例
		processEngine.getRuntimeService().startProcessInstanceByKey(defKey);
	}
	
	@Test
	/**
	 * 处理任务---张三提交个人任务----流向任务组
	 */
	public void dealTask(){
		//先查询个人待处理任务
		TaskQuery taskQuery = processEngine.getTaskService().createTaskQuery();
		//通过登录用户名查询自己的待处理任务
		taskQuery.taskAssignee("张三");
		List<Task> list = taskQuery.list();
		//获取任务id,因为只有一个任务,所有直接获得
		String id = list.get(0).getId();
		System.out.println(list.get(0).getName());
		//根据任务id处理任务
		processEngine.getTaskService().complete(id);
	}
	
	@Test
	/*
	 * 任务组拾取任务
	 */
	public void getTask(){
		TaskQuery taskQuery = processEngine.getTaskService().createTaskQuery();
		//任务组的人获取任务
		taskQuery.taskCandidateUser("张三");
		List<Task> list = taskQuery.list();
		String id=list.get(0).getId();//获取任务id
		//再将组任务变成自己的任务
		processEngine.getTaskService().claim(id, "张三");//李四拾取的组任务,王五就不能再拾取该任务了.
	}
	@Test
	/*
	 *放弃任务--退回组任务
	 */
	public void dropTask(){
		processEngine.getTaskService().setAssignee("1702", null);
	}
	
	
}
